﻿using System;
using System.Collections.Generic;
using System.Text;
using SH;

namespace MTS
{
    class mtSysManageServer : mtInterface, ImtSysManagerServer
    {
        public mtSysManageServer()
              : base("mtSysManageServer")
        {

        }

        bool _func(mtGWFTask wfTask, emWFTaskItemType type)
        {
            var taskList = wfTask.ListTaskItem;

            foreach (var taskItem in taskList)
            {
                if (taskItem.TaskItemType == type)
                    return true;
            }
            return false;
        }


        shErrorCode ImtSysManagerServer.CancleGWorkFlowTask(mtGWFTask wfTask)
        {
            if (m_pHost.ProgramType != emProgramType.emPT_SMS)
            {
                return shErrorCode.sh_ec_AccessDenied;
            }

            m_pHost.RunEnv.UpdateGWorkFlowTask(wfTask);

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Scan) || _func(wfTask, emWFTaskItemType.emWFTIT_Calib))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageCancleGWorkFlowTask, emProgramType.emPT_SCS, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcc))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageCancleGWorkFlowTask, emProgramType.emPT_RCC, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcg))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageCancleGWorkFlowTask, emProgramType.emPT_RCG, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcs) || _func(wfTask, emWFTaskItemType.emWFTIT_Rcg) || _func(wfTask, emWFTaskItemType.emWFTIT_Rcc))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageCancleGWorkFlowTask, emProgramType.emPT_CSS, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_IPS))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageCancleGWorkFlowTask, emProgramType.emPT_IPSS, sc);
            }

            return shErrorCode.sh_ec_Success;
        }

        shErrorCode ImtSysManagerServer.GetCredential(string token, ref mtCredential crd)
        {
            if (m_pHost.ProgramType == emProgramType.emPT_SMS)
            {
                return shErrorCode.sh_ec_InvalidOperation;
            }

            MTSSerializer sc = new MTSSerializer();

            sc.serialize(ref token);

            var ec = __InvokeTwoWay(ImtSMSServerCallback._classID, ImtSMSServerCallback.SMSServerGetCredential, token, ref sc);

            if (ec != shErrorCode.sh_ec_Success)
            {
                crd = null;
                return ec;
            }

            crd = new mtCredential();
            sc.serialize(crd);

            return ec;
        }

        shErrorCode ImtSysManagerServer.KickOut(string token)
        {

            MTSSerializer sc = new MTSSerializer();

            sc.serialize(ref token);

            return __InvokeOneWay(0, 10, sc);

        }

        shErrorCode ImtSysManagerServer.PublishGSystemStatus(mtGSystemStatus wfSystemStatus)
        {
            if (m_pHost.ProgramType == emProgramType.emPT_SMS)
            {
                m_pHost.RunEnv.UpdateGSystemRunStatus(wfSystemStatus);

                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfSystemStatus);

                return __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManagePublishGSystemStatus, emProgramType.emPT_ALL, sc);

            }
            else
                return shErrorCode.sh_ec_AccessDenied;
        }

        shErrorCode ImtSysManagerServer.PublishGWorkFlowTask(mtGWFTask wfTask)
        {
            if (m_pHost.ProgramType != emProgramType.emPT_SMS)
            {
                return shErrorCode.sh_ec_AccessDenied;
            }

            m_pHost.RunEnv.UpdateGWorkFlowTask(wfTask);

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Scan) || _func(wfTask, emWFTaskItemType.emWFTIT_Calib))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManagePublishGWorkFlowTask, emProgramType.emPT_SCS, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcc))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManagePublishGWorkFlowTask, emProgramType.emPT_RCC, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcg))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManagePublishGWorkFlowTask, emProgramType.emPT_RCG, sc);
            }

            // 正常扫包 现在是两个存储（RCS、CSS）都发
            if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcs) || _func(wfTask, emWFTaskItemType.emWFTIT_Rcg) || _func(wfTask, emWFTaskItemType.emWFTIT_Rcc))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManagePublishGWorkFlowTask, emProgramType.emPT_CSS, sc);
            }

            // 再重建、再识别发送存储任务
            //if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcs))
            //{
            //    MTSSerializer sc = new MTSSerializer();
            //    sc.serialize(wfTask);

            //    __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManagePublishGWorkFlowTask, emProgramType.emPT_CSS, sc);
            //}

            if (_func(wfTask, emWFTaskItemType.emWFTIT_IPS))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManagePublishGWorkFlowTask, emProgramType.emPT_IPSS, sc);
            }

            return shErrorCode.sh_ec_Success;
        }


        shErrorCode ImtSysManagerServer.RunStatusChange(string token, ref uint status)
        {
            MTSSerializer sc = new MTSSerializer();

            return __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageRunStatusChange, token, sc);
        }


        shErrorCode ImtSysManagerServer.Start()
        {
            var host = GetVHost();

            return host.Start();
        }

        shErrorCode ImtSysManagerServer.StatusChange(string token, ref ulong status)
        {
            MTSSerializer sc = new MTSSerializer();

            sc.serialize(ref status);

            return __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageStatusChange, token, sc);
        }

        shErrorCode ImtSysManagerServer.Stop()
        {
            var host = GetVHost();

            return host.Stop();
        }

        shErrorCode ImtSysManagerServer.UpdateGWorkFlowTask(mtGWFTask wfTask)
        {
            if (m_pHost.ProgramType != emProgramType.emPT_SMS)
            {
                return shErrorCode.sh_ec_AccessDenied;
            }

            m_pHost.RunEnv.UpdateGWorkFlowTask(wfTask);


            if (_func(wfTask, emWFTaskItemType.emWFTIT_Scan) || _func(wfTask, emWFTaskItemType.emWFTIT_Calib))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageUpdateGWorkFlowTask, emProgramType.emPT_SCS, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcc))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageUpdateGWorkFlowTask, emProgramType.emPT_RCC, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcg))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageUpdateGWorkFlowTask, emProgramType.emPT_RCG, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_Rcs) || _func(wfTask, emWFTaskItemType.emWFTIT_Rcg) || _func(wfTask, emWFTaskItemType.emWFTIT_Rcc))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageUpdateGWorkFlowTask, emProgramType.emPT_CSS, sc);
            }

            if (_func(wfTask, emWFTaskItemType.emWFTIT_IPS))
            {
                MTSSerializer sc = new MTSSerializer();
                sc.serialize(wfTask);

                __InvokeOneWay(ImtSysManageCallback._classID, ImtSysManageCallback.SysManageUpdateGWorkFlowTask, emProgramType.emPT_IPSS, sc);
            }

            return shErrorCode.sh_ec_Success;
        }

        shErrorCode ImtSysManagerServer.GetCurCredential(ref mtCredential crd)
        {
            MTSSerializer sc = new MTSSerializer();

            var ec = __InvokeTwoWay(0, 11, ref sc);

            if (ec != shErrorCode.sh_ec_Success)
            {
                crd = null;
                return ec;
            }

            crd = new mtCredential();
            sc.serialize(crd);

            return ec;
        }

        shErrorCode ImtSysManagerServer.GetCurLogonInfo(ref mtLogOnInfo info)
        {
            MTSSerializer sc = new MTSSerializer();

            var ec = __InvokeTwoWay(0, 12, ref sc);

            if (ec != shErrorCode.sh_ec_Success)
            {
                info = null;
                return ec;
            }

            info = new mtLogOnInfo();
            sc.serialize(info);

            return ec;
        }

        shErrorCode ImtSysManagerServer.GetCurStationInfo(ref mtStationInfo info)
        {
            MTSSerializer sc = new MTSSerializer();

            var ec = __InvokeTwoWay(0, 13, ref sc);

            if (ec != shErrorCode.sh_ec_Success)
            {
                info = null;
                return ec;
            }

            info = new mtStationInfo();
            sc.serialize(info);

            return ec;
        }

        string ImtSysManagerServer.GetCurToken()
        {
            MTSSerializer sc = new MTSSerializer();

            var ec = __InvokeTwoWay(0, 14, ref sc);

            if (ec != shErrorCode.sh_ec_Success)
            {
                return "";
            }

            string token = null;
            sc.serialize(ref token);

            return token;
        }
    }
}
